package com.android.delivery.adapter

import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import com.android.delivery.R
import com.android.delivery.entity.print.BluetoothInfoBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

/**
 * 打印-云打印
 * @author: mqwang
 * @create: 3/18/21 1:33 PM
 * @Description:
 */
class PrintBlueAdapter : BaseQuickAdapter<BluetoothInfoBean, BaseViewHolder>(R.layout.adapter_print_cloud) {

    private var mListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mListener = listener
    }

    override fun convert(helper: BaseViewHolder?, item: BluetoothInfoBean?) {

        helper!!.setText(R.id.tv_name, item!!.name)

        val connectTv = helper.getView<TextView>(R.id.tv_connect)
        val connectPb = helper.getView<ProgressBar>(R.id.conn_pb)
        if (item.connected == BluetoothInfoBean.CONNECTING) {
            connectPb.visibility = View.VISIBLE
            connectTv.visibility = View.GONE
        } else if (item.connected == BluetoothInfoBean.CONNECTED) {
            connectTv.setText("已连接")
            connectTv.setTextColor(mContext.resources.getColor(R.color.color_FF832C))
            connectTv.background = null
            connectPb.visibility = View.GONE
            connectTv.visibility = View.VISIBLE
        } else {
            connectTv.setText("连接")
            connectTv.setTextColor(mContext.resources.getColor(R.color.colorWhite))
            connectTv.setBackgroundResource(R.drawable.bg_btn)
            connectPb.visibility = View.GONE
            connectTv.visibility = View.VISIBLE
        }
        connectTv.setOnClickListener {
            if (item.connected == BluetoothInfoBean.DISCONNECT) {
                mListener?.connDevice(item)
            }
        }
        helper.itemView.setOnClickListener {
            if (item.connected == BluetoothInfoBean.CONNECTED) {
                mListener?.onPrint(item)
            }
        }
    }

    fun updateConnStatus(conn: Int, bean: BluetoothInfoBean) {
        for (blueInfo in mData) {
            if (blueInfo.name.equals(bean.name)) {
                blueInfo.connected = conn
            }
        }
        notifyDataSetChanged()
    }

    fun connectDefault(device: BluetoothInfoBean) {
        mListener?.connDevice(device)
    }

    interface OnItemClickListener {
        fun connDevice(device: BluetoothInfoBean)
        fun onPrint(device: BluetoothInfoBean)
    }
}